package com.itheima.app.controller;

import com.itheima.app.manager.SettingManager;
import com.itheima.app.manager.UserManager;
import com.itheima.domain.db.Notification;
import com.itheima.domain.db.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collection;
import java.util.Map;

@RestController
public class SettingController {

    @Autowired
    SettingManager settingManager;

    @Autowired
    UserManager userManager;

    @GetMapping("/users/settings")
    public ResponseEntity findSetting() {
        return settingManager.findSetting();
    }

    // 设置陌生人问题
    @PostMapping("/users/questions")
    public void setQuestion(@RequestBody Map<String, String> param) {
        // 1.接收参数
        String content = param.get("content");
        // 2.调用manager
        settingManager.setQuestion(content);
    }

    // 设置推送通知
    @PostMapping("/users/notifications/setting")
    public void setNotification(@RequestBody Notification param) {
        // 调用manager
        settingManager.setNotification(param);
    }

    // 黑名单分页查询
    @GetMapping("/users/blacklist")
    public ResponseEntity findBlackListByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        // 调用manager
        return settingManager.findBlackListByPage(pageNum, pageSize);
    }

    //移除黑名单
    @DeleteMapping("/users/blacklist/{blackId}")
    public void deleteBlackList(@PathVariable Long blackId) {
        settingManager.deleteBlackList(blackId);
    }

    //发送短信验证码
    @PostMapping("/users/phone/sendVerificationCode")
    public void sendVerificationCode(@RequestHeader("Authorization") String token) {
        //1.从token中获取用户的号码
        User user = userManager.findUserFromToken(token);
        String phone = user.getMobile();
        userManager.sendCode(phone);
    }

    //校验验证码
    @PostMapping("/users/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestHeader("Authorization") String token, @RequestBody Map<String, String> param) {
        //1.从token中获取用户的号码
        User user = userManager.findUserFromToken(token);
        String phone = user.getMobile();
        String code = param.get("verificationCode");
        return settingManager.checkVerificationCode(code, phone);
    }


    //修改手机号
    @PostMapping("/users/phone")
    public void updatephone(@RequestHeader("Authorization") String token, @RequestBody Map<String,String>param) {
        //1.从token中当前用户的id
        User user = userManager.findUserFromToken(token);
        Long userId = user.getId();
        String phone = param.get("phone");
        userManager.updatephone(phone, userId);
    }
}